<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends CI_Controller{
	public $client = '';
	public $arrData = '';
	public function __construct() {
		parent::__construct();
		$this->output->set_header("Content-type: text/html; charset=utf-8");
		$this->load->model('Comm');
		$this->client = 'web/';
		$arrUserLogin = json_decode($this->Comm->getUserCookie(), true);
		$this->arrData['user_login'] = $arrUserLogin;
		$this->arrData['siteinfo'] = $this->Comm->getTableCache('siteinfo', array('sid'=>1), true);
		$this->arrData['user_info'] = $this->Comm->getUserInfo($arrUserLogin['uid']);
	}

	public function index()	{
		$this->arrData['channel_depth'] = 0;
		$this->arrData['channel'] = $this->Comm->getChannel();
		$this->arrData['marketing'] = $this->Comm->getTableCache('marketing', NULL, false, array('mid', 'ad0'));
		$this->arrData['recommend'] = $this->Comm->getTableCache('marketing', NULL, false, array('mid', 'recommend0'));
		$this->arrData['__META'] = $this->Comm->getMeta('首页');
		$this->load->view($this->client . 'index', $this->arrData);
	}
	
	public function logout() {
		$this->Comm->delCookie();
		$this->Comm->delCookie('user_info');
		$this->load->helper('url');
		redirect('index.html', 'location', 302);
	}
	public function login() {
		$this->arrData['message'] = $this->input->get('msg');
		$this->load->helper('form');
		$this->arrData['use_validate'] = '';
		$etag = $this->Comm->getEtag();
		$referer_post = $this->input->post('referer') != '' ? $this->input->post('referer') : $this->input->get('referer');
		$referer = $this->Comm->getReferUrl();
		$this->arrData['referer'] = !empty($referer_post) ? $referer_post : $referer;
		if($this->input->post('__H') == 'userLogin' && ($email = $this->input->post('email')) != "" 
			&& ($passwd = $this->input->post('password')) != "") {
			$this->load->model('User_interface');
			$validate = $this->Comm->getCookie('validate');
			$passwd = md5(md5($passwd) . $this->config->item('user_key'));
			$arrUserLogin = $this->Comm->getRow('user_register', 'uid, nick_name', array('email'=>$email, 'passwd'=>$passwd));
			if(isset($arrUserLogin['uid']) && $arrUserLogin['uid'] > 0) {
				$this->Comm->setCookie(json_encode($arrUserLogin));
				$this->load->helper('url');
				$redirect_url = str_replace(array('index.html','login.html','register.html'), '', $this->arrData['referer']) == $this->config->base_url() ? '/user/home' : $this->arrData['referer'];
				$redirect_url = str_replace($this->config->base_url(), '', $redirect_url);
				redirect($redirect_url, 'location', 302);
			} else {				
				$this->arrData['message'] = '登录失败，请检查用户名和密码！';
				//$this->load->library('encrypt');
				//print_r($_SERVER);
				if(empty($etag)) {
					$this->Comm->setEtag(base64_encode(1));
				} else {
					$etag = base64_decode($etag);
					if($etag >= 3) $this->arrData['use_validate'] = '1';
					$etag++;
				}
			}
		}
		if(!empty($etag)) $this->Comm->setEtag(base64_encode($etag));
		$this->arrData['__META'] = $this->Comm->getMeta('用户登录');
		$this->load->view($this->client . 'login', $this->arrData);
	}

	public function register() {
		$this->arrData['message'] = '';
		if($this->input->post('__H') == 'newUser') {
			$arrJson = json_decode($this->newUser_interface(true), true);
			if($arrJson['uid'] > 0) {
				$this->load->helper('url');
				redirect('/user/home', 'location', 302);
			} else {
				$errortype['-1'] = '用户已经存在';
				$errortype['-2'] = '2次输入的密码不等';
				$errortype['-3'] = '插入失败';
				$errortype['-4'] = '验证不通过，非法递交';
				$errortype['-5'] = 'email不能为空，非法递交';
				$err_message = isset($errortype[$arrJson['uid']]) ? $errortype[$arrJson['uid']] : '没定义错误信息！';
				$this->arrData['message'] = '注册失败，' . $err_message;
			}
		}
		$this->arrData['__META'] = $this->Comm->getMeta('用户注册');
		$this->load->helper('form');
		$this->load->view($this->client . 'register', $this->arrData);
	}
	
	public function chkemail_interface($return = false) {
		$this->load->model('User_interface');
		$userid = $this->User_interface->checkUserRegister();
		if($return) return json_encode(array('uid'=>$userid));
		echo json_encode(array('uid'=>$userid));
	}
	
	public function newUser_interface($return = false) {
		$this->load->model('User_interface');
		$userid = $this->User_interface->newUser();
		$cookie_value = NULL;
		if($userid > 0) {
			$cookie_value = json_encode(array('uid'=>$userid, 'nick_name'=>$this->input->post('nick_name')));
			$this->Comm->setCookie($cookie_value);
		}
		if($return) return $cookie_value;
		echo $cookie_value;
	}
	
	public function validate_interface($return = false) {
		$this->load->library('valiimage');
		$validate = $this->valiimage->validateCode(4);
		$this->Comm->setCookie($validate, 'validate');
		$this->valiimage->generateValidationImage($validate);
	}
	
}

/* End of file index.php */
/* Location: ./application/controllers/index.php */